<template>
  <div class="app-container">
    <transition name="el-zoom-in-top">
      <el-form
        :inline="true"
        size="small"
        ref="queryForm"
        v-show="showSearch"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item label="文章标题">
          <el-input
            @keyup.enter.native="handleQuery"
            v-model="queryParams.title"
            style="width: 230px"
            placeholder="请输入文章标题"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="文章状态">
          <el-select v-model="queryParams.status" placeholder="文章状态" clearable>
            <el-option
              v-for="dict in dict.type.gonitis_education_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="患者来源">
          <el-cascader
            style="width: 230px"
            :props="defaultProps"
            @change="handleSubChange"
            clearable
            :options="deptOptions"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 230px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getDataList"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </transition>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addOrUpdateHandle(0)"
          v-hasPermi="['gonitis:education:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="updateHandle()"
          v-hasPermi="['gonitis:education:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="deleteHandle(0)"
          v-hasPermi="['gonitis:education:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getDataList"
      ></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="50"></el-table-column>
      <el-table-column prop="id" align="center" label="文章编号"></el-table-column>
      <el-table-column prop="doctorId" align="center" label="医生编号"></el-table-column>
      <el-table-column prop="doctorName" align="center" width="100px" label="医生姓名">
        <template slot-scope="scope">
          <span>{{ scope.row.doctorName ? scope.row.doctorName : "暂无" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="subTitle" align="center" width="100px" label="患者来源">
        <template slot-scope="scope">
          <span>{{ scope.row.subTitle ? scope.row.subTitle : "暂无" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="auditorId"
        header-align="center"
        align="center"
        label="审核人"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.auditorName ? scope.row.auditorName : "暂无" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        align="center"
        width="100px"
        label="标题"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column prop="ossId" width="120px" align="center" label="默认图片">
        <template slot-scope="scope">
          <ImagePreview
            :width="100"
            :height="100"
            :src="scope.row.defaultPic"
            :preview-src-list="[scope.row.defaultPic]"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="publishTime"
        header-align="center"
        width="100px"
        align="center"
        label="发布时间"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="100px"
        label="创建时间"
      ></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.gonitis_education_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        class-name="small-padding fixed-width"
        width="160"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            :disabled="!(scope.row.status === 2)"
            v-hasPermi="['gonitis:education:edit']"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="mini"
            icon="el-icon-delete"
            v-hasPermi="['gonitis:education:remove']"
            :disabled="!(scope.row.status === 2)"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
          <el-dropdown
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
          >
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right"
              >更多</el-button
            >
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="getDetail"
                icon="el-icon-view"
                v-hasPermi="['gonitis:education:query']"
                >详情</el-dropdown-item
              >
              <el-dropdown-item
                :disabled="scope.row.status === 2"
                command="auditHandle"
                icon="el-icon-circle-check"
                v-hasPermi="['gonitis:education:audit']"
                >审核</el-dropdown-item
              >
              <el-dropdown-item
                command="updateStatus"
                :icon="
                  scope.row.status === 2
                    ? 'el-icon-circle-plus-outline'
                    : 'el-icon-remove-outline'
                "
                v-hasPermi="['gonitis:education:edit']"
                >{{ scope.row.status === 2 ? "启用" : "停用" }}</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />
    <edit v-if="editVisible" ref="edit" @getDataList="getDataList"></edit>
  </div>
</template>

<script>
import Edit from "./edit";
import { subTree } from "@/api/gonitis/illSub";
import { listEducation, updateStatus, removeEducation } from "@/api/gonitis/education";
export default {
  name: "Education",
  dicts: ["gonitis_education_status"],
  data() {
    return {
      // 病情多选框映射
      defaultProps: {
        children: "children",
        label: "label",
        value: "id",
      },
      // 病情列表
      deptOptions: [],
      editVisible: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        subId: undefined,
        status: undefined,
      },
      dateRange: [],
      dataList: [],
      total: 0,
      loading: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 多选id
      ids: [],
    };
  },
  components: {
    Edit,
  },
  created() {
    this.init();
  },
  methods: {
    /** 页面初始化 */
    init() {
      this.getDataList();
      this.getDeptTree();
    },
    /** 病情分类选择框改变 */
    handleSubChange(value) {
      this.queryParams.subId = value[value.length - 1];
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      subTree().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 更多操作触发 */
    handleCommand(command, row) {
      switch (command) {
        case "getDetail":
          this.getDetail(row.id);
          break;
        case "auditHandle":
          this.auditHandle(row.id);
          break;
        case "updateStatus":
          this.updateStatus(row.status, row.id);
          break;
        default:
          break;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getDataList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.title = undefined;
      this.queryParams.subId = undefined;
      this.queryParams.status = undefined;
      this.handleQuery();
    },
    /** 更新状态 */
    updateStatus(currentStatus, currentId) {
      if (currentStatus === 2) {
        currentStatus = 0;
      } else {
        currentStatus = 2;
      }
      const statusVo = {
        id: currentId,
        status: currentStatus,
      };
      updateStatus(statusVo).then((response) => {
        this.$message({
          message: "操作成功",
          type: "success",
          duration: 1500,
        });
        this.getDataList();
      });
    },
    /** 获取数据列表 */
    getDataList() {
      this.loading = true;
      listEducation(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.dataList = response.data.list;
          this.total = response.data.totalCount;
          this.loading = false;
        }
      );
    },
    /** 多选处理 */
    selectionChangeHandle(selection) {
      this.multiple = !selection.length;
      this.single = selection.length !== 1;
      this.ids = selection.map((item) => {
        if (item.status != 2) {
          this.single = true;
          this.multiple = true;
        }
        return item.id;
      });
    },
    /** 获取详情 */
    getDetail(value) {
      const isAudit = 2;
      this.editVisible = true;
      this.$nextTick(() => {
        this.$refs.edit.init(value, isAudit);
      });
    },
    /** 新增 / 修改 */
    addOrUpdateHandle(value) {
      const isAudit = 0;
      this.editVisible = true;
      this.$nextTick(() => {
        this.$refs.edit.init(value, isAudit);
      });
    },
    /** 多选修改 */
    updateHandle() {
      this.editVisible = true;
      const isAudit = 0;
      const id = this.ids[0];
      this.$nextTick(() => {
        this.$refs.edit.init(id, isAudit);
      });
    },
    /** 审核 */
    auditHandle(value) {
      const isAudit = 1;
      this.editVisible = true;
      this.$nextTick(() => {
        this.loading = true;
        this.$refs.edit.init(value, isAudit);
      });
    },
    /** 删除 */
    deleteHandle(id) {
      const ids = id ? [id] : this.ids;
      this.$modal
        .confirm('是否确认删除【教育管理】编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return removeEducation(ids);
        })
        .then(() => {
          this.getDataList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>
